﻿/****************************************************************************
 * 模    块	：枚举类型扩展 工具类
 * 作    者	：Jasper
 * 编写时间	：2008-11-27
 * 实现功能	：读出枚举的标签内容
 * 详细描述	：
 ****************************************************************************/

namespace Basic4Net.Utility
{
	using System;
	using System.Reflection;
	using System.Data;

	public class TextAttributeTools
	{		
		/// <summary>
		/// 获取枚举的TextAttribute
		/// </summary>
		/// <param name="enumConst">枚举值</param>
		/// <param name="enumType">枚举类型</param>
		/// <returns>当前值的枚举的TextAttribute</returns>
		public static string GetEnumTextVal(int enumConst, Type enumType)
		{
			if (!enumType.IsEnum)
			{
				throw new InvalidOperationException();
			}

			string textVal = String.Empty;

			Type typeDescription = typeof(TextAttribute);
			FieldInfo fieldInfo = enumType.GetField(Enum.GetName(enumType, enumConst).ToString());

			if (fieldInfo != null)
			{
				object[] arr = fieldInfo.GetCustomAttributes(typeDescription, true);
				if (arr.Length > 0)
				{
					TextAttribute textAttribute = (TextAttribute)arr[0];
					textVal = textAttribute.Text;
				}
			}

			return textVal;
		}


		/// <summary>
		/// 获取枚举的DataTable，DataTable包含Text和Value两列
		/// </summary>
		/// <param name="enumType"></param>
		/// <returns></returns>
		public static DataTable GetEnumTable(Type enumType)
		{
			if (!enumType.IsEnum)
			{
				throw new InvalidOperationException();
			}

			DataTable dt = new DataTable();
			dt.Columns.Add("Text", typeof(System.String));
			dt.Columns.Add("Value", typeof(System.String));

			Type typeDescription = typeof(TextAttribute);

			FieldInfo[] fields = enumType.GetFields();

			foreach (FieldInfo field in fields)
			{
				if (field.FieldType.IsEnum == true)
				{
					DataRow dr = dt.NewRow();

					dr["Value"] = ((int)enumType.InvokeMember(field.Name, BindingFlags.GetField, null, null, null)).ToString();

					object[] arr = field.GetCustomAttributes(typeDescription, true);
					if (arr.Length > 0)
					{
						TextAttribute aa = (TextAttribute)arr[0];
						dr["Text"] = aa.Text;
					}
					else
					{
						dr["Text"] = field.Name;
					}
					dt.Rows.Add(dr);
				}
			}

			return dt;
		}


		/// <summary>
		/// 获取枚举的DataTable，DataTable包含Text和Value两列
		/// </summary>
		/// <param name="enumType"></param>
		/// <returns></returns>
		public static DataTable GetEnumTableForText(Type enumType)
		{
			if (!enumType.IsEnum)
			{
				throw new InvalidOperationException();
			}

			DataTable dt = new DataTable();
			dt.Columns.Add("Text", typeof(System.String));
			dt.Columns.Add("Value", typeof(System.String));

			FieldInfo[] fields = enumType.GetFields();

			foreach (FieldInfo field in fields)
			{
				if (field.FieldType.IsEnum == true)
				{
					DataRow dr = dt.NewRow();

					dr["Value"] = ((int)enumType.InvokeMember(field.Name, BindingFlags.GetField, null, null, null)).ToString();
					dr["Text"] = enumType.InvokeMember(field.Name, BindingFlags.GetField, null, null, null).ToString();
					dt.Rows.Add(dr);
				}
			}


			return dt;
		}
	}
}
